//
//  LCjockCoCell.m
//  TheLCNews
//
//  Created by apple on 15/12/1.
//  Copyright © 2015年 LC. All rights reserved.
//

#import "LCjockCoCell.h"

@implementation LCjockCoCell


#pragma mark 初始化方法
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
         self.webDateArr = [NSMutableArray array];
        self.page = 1;
    }
    return self;
}

#pragma mark 设置view样式
- (void)createView{
    self.tableView = [[UITableView alloc] init];
    [self.contentView addSubview:self.tableView];
    
    // 可以把线tableView的线去掉
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self getTitleNameParse];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoStateFooter footerWithRefreshingBlock:^{
        [self fotter];
    }];
    self.tableView.showsVerticalScrollIndicator = NO;
    
    [self getTitleNameParse];
    
}

#pragma mark 设置布局
- (void)layoutSubviews{
    [super layoutSubviews];
    self.tableView.frame = CGRectMake(0, 0, KWIDTH, KHEIGHT - 64 - 49);
}



#pragma mark 重写的set方法
- (void)setTitle:(NSString *)title{
    if (_title != title) {
        [self addHeader:title];
        [self addFooter:title];
    }
}



#pragma mark 下拉刷新
- (void)addHeader:(NSString *)title{
//    __block TypeThreeCollectionViewCell *typeThree = self;
//    typeThree.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//        typeThree.nextPage = 1;
//        [typeThree getTitleNameParse:typeThree.nextPage withTitle:title];
//    }];
//    // 要添加此步才能自行自动刷新
//    [typeThree.tableView.header beginRefreshing];
}

#pragma mark 上拉加载
- (void)addFooter:(NSString *)title{
//    __unsafe_unretained TypeThreeCollectionViewCell *typeThree = self;
//    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//        typeThree.nextPage++;   // 每次要加载新页面所以++
//        [typeThree getTitleNameParse:typeThree.nextPage withTitle:title];
//    }];
}

#pragma mark 数据解析
- (void)getTitleNameParse{
//        AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
////        // 开始检测
////        [manager startMonitoring];
//        // 检测网络方法
//        [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
    
    self.hud = [[MBProgressHUD alloc] initWithView:self.tableView];
    [self.contentView addSubview:_hud];
    [self.contentView bringSubviewToFront:_hud];
    //self.hud.delegate = self;
    self.hud.labelText = @"加载中...";
    [self.hud show:YES];

            [LYLAFNetWorking GETURLByString:@"http://api.3g.ifeng.com/clientShortNews?type=joke&page=1&gv=4.4.8&av=4.4.8&uid=867588027089230&deviceid=867588027089230&proid=ifengnews&os=android_19&df=androidphone&vt=5&screen=720x1280&publishid=9023" AFData:^(id result) {
                NSDictionary *dic = result;
                               for (NSDictionary *tempDic in dic[@"body"]) {
                    LCInformationModel *model = [[LCInformationModel alloc] init];
                    [model setValuesForKeysWithDictionary:tempDic];
                    // 因为img数组没有图片时为空,会导致报错,所以需要添加此步才能把数据存到model中
                    if ([tempDic[@"img"] count] != 0) {
                        [model setValue:tempDic[@"img"][0][@"size"][@"width"] forKey:@"width"];
                        [model setValue:tempDic[@"img"][0][@"size"][@"height"] forKey:@"height"];
                        [model setValue:tempDic[@"img"][0][@"url"] forKey:@"url"];
                         NSLog(@"6666%@",model);
                    }
                    [self.webDateArr addObject:model];
                     NSLog(@"555555%@",self.webDateArr);
                }
                        if (self.webDateArr.count > 0) {
                        [self.tableView reloadData];
        }
                [self.hud removeFromSuperview];
                [self.tableView.mj_header endRefreshing];
        }];
//        }];
}

       




#pragma mark tableView协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   
        return self.webDateArr.count;
    
    
         }
         


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LCInformationModel *model = self.webDateArr[indexPath.row];
    
    static NSString *reuse = @"reuse";
    LCjackTbCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[LCjackTbCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    [cell.thumbnailImageView sd_setImageWithURL:[NSURL URLWithString:model.url] placeholderImage:[UIImage imageNamed:@"zhanwei"]];
    cell.contentLabel.text = model.content;
    cell.commentsLabel.text = [NSString stringWithFormat:@"评论:%@",[model.comments stringValue]];
    cell.likesLabel.text = [NSString stringWithFormat:@"赞: %@", model.likes];
    
    
    // 把高的值传到cell页面
    cell.height = model.height;
    // 把number传到cell页面
    cell.likes = model.likes;
    
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    LCInformationModel *model = self.webDateArr[indexPath.row];
    // 图片
    CGFloat picHeight = [model.height floatValue];
    
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17.], NSFontAttributeName, nil];
    CGRect rect = [model.content boundingRectWithSize:CGSizeMake(355, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    if (model.content.length != 0) {
        return picHeight + rect.size.height + KHEIGHT / 10.26153846153846;
    }
    else{
        return picHeight + KHEIGHT / 11.1166666666667;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   // LCInformationModel *model = self.webDateArr[indexPath.row];
    //[self.jockDelegate setURLByCollectionView:model];
}

#pragma mark - 上啦加载
- (void)fotter
{
    self.page ++;
   
    [LYLAFNetWorking GETURLByString:[NSString stringWithFormat:@"http://api.3g.ifeng.com/clientShortNews?type=joke&page=%ld&gv=4.4.8&av=4.4.8&uid=867588027089230&deviceid=867588027089230&proid=ifengnews&os=android_19&df=androidphone&vt=5&screen=720x1280&publishid=9023",self.page] AFData:^(id result) {
        NSDictionary *dic = result;
       
        for (NSDictionary *tempDic in dic[@"body"]) {
            LCInformationModel *model = [[LCInformationModel alloc] init];
            [model setValuesForKeysWithDictionary:tempDic];
            // 因为img数组没有图片时为空,会导致报错,所以需要添加此步才能把数据存到model中
            if ([tempDic[@"img"] count] != 0) {
                [model setValue:tempDic[@"img"][0][@"size"][@"width"] forKey:@"width"];
                [model setValue:tempDic[@"img"][0][@"size"][@"height"] forKey:@"height"];
                [model setValue:tempDic[@"img"][0][@"url"] forKey:@"url"];
                NSLog(@"6666%@",model);
            }
            NSMutableArray *arr = [NSMutableArray array];
            [arr addObject:model];
            [self.webDateArr addObjectsFromArray:arr];
            NSLog(@"555555%@",self.webDateArr);
        }
        if (self.webDateArr.count > 0) {
            [self.tableView reloadData];
        }
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
    }];

}






/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
